package java_class_work.week08.demo02;

/**
 * @Author Tiam
 * @Date 2022/10/31 11:24
 * @Description:
 */
public class Box {
    private int product;

    public synchronized void addProduct(){
        while (true) {
            if(product>=20){
                // 当产量达到20时, 进入就绪态, 等待执行
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println(Thread.currentThread().getName()+" 生产了"+(++product)+"号奶");
                // 唤醒全部进程
                notifyAll();
            }
        }
    }
    public synchronized void getProduct(){
        while (true) {
            if(product<=0){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println(Thread.currentThread().getName()+" 消费了"+product--+"号奶");
                notifyAll();
            }
        }
    }
}
